//获取应用实例
const app = getApp()
Page({
  data: {
    height: '', // 获取当前页面的可视高度
    group:
      [{ src: "https://img3.doubanio.com/view/subject/l/public/s1727290.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/l/public/s29053580.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/l/public/s26012674.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/l/public/s28295535.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/m/public/s29954100.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/m/public/s29946573.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img1.doubanio.com/view/photo/m_ratio_poster/public/p2543464369.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img1.doubanio.com/view/photo/m_ratio_poster/public/p2541280047.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2544510053.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2536564565.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/l/public/s1727290.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/l/public/s29053580.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/l/public/s26012674.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/l/public/s28295535.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/m/public/s29954100.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/m/public/s29946573.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/m/public/s29954100.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img1.doubanio.com/view/photo/m_ratio_poster/public/p2543464369.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img1.doubanio.com/view/photo/m_ratio_poster/public/p2541280047.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2544510053.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2536564565.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/l/public/s1727290.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/l/public/s29053580.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/l/public/s26012674.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/l/public/s28295535.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/m/public/s29954100.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/m/public/s29946573.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img1.doubanio.com/view/photo/m_ratio_poster/public/p2543464369.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img1.doubanio.com/view/photo/m_ratio_poster/public/p2541280047.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2544510053.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2536564565.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/m/public/s29954100.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/m/public/s29954100.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/m/public/s29954100.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/m/public/s29954100.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/m/public/s29954100.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img1.doubanio.com/view/photo/m_ratio_poster/public/p2543464369.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img1.doubanio.com/view/photo/m_ratio_poster/public/p2541280047.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2544510053.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2536564565.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/l/public/s29053580.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/l/public/s26012674.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/l/public/s28295535.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/m/public/s29954100.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/m/public/s29946573.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img1.doubanio.com/view/photo/m_ratio_poster/public/p2543464369.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img1.doubanio.com/view/photo/m_ratio_poster/public/p2541280047.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2544510053.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2536564565.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/l/public/s29053580.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/l/public/s26012674.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/l/public/s28295535.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/m/public/s29954100.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/subject/m/public/s29946573.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img1.doubanio.com/view/photo/m_ratio_poster/public/p2543464369.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img1.doubanio.com/view/photo/m_ratio_poster/public/p2541280047.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2544510053.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" },
      { src: "https://img3.doubanio.com/view/photo/m_ratio_poster/public/p2536564565.jpg", show: false, def: "https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif" }]
  },
  onLoad: function () {
    wx.getSystemInfo({  // 获取页面可视区域的高度
      success: (res) => {
        this.setData({
          height: res.screenHeight
        })
      },
    })
    this.showImg()
    let group = this.data.group  // 获取原数据
    for (let i in this.data.group) {
      wx.createIntersectionObserver().relativeToViewport({ bottom: 20 }).observe('.poster-' + i, (ret) => {
        console.log(ret)
        if (ret.intersectionRatio > 0) {
          group[i].show = true
        }
        this.setData({ // 更新数据
          group
        })
      })
    }

  },
  onPageScroll() {  // 调用showImg函数
    app.Util.debounce(this.showImg())
  },

  showImg() {  // 判断高度是否需要加载
    wx.createSelectorQuery().selectAll('.poster').boundingClientRect((ret) => {
      const group = this.data.group
      const height = this.data.height
      ret.forEach((item, index) => {
        if (item.top < height) {
          group[index].show = true
        }
      })
      this.setData({
        group
      })
    }).exec()
  }
})
